import { getToken } from '@/utils/auth'
import envConfig from '@/utils/env'


const createRequest = (method) => (url, data = {}, config = {}) => {
  // 合并 headers
  const headers = {
    "Content-Type": "application/json",
    "Authorization": 'Bearer ' + getToken(),
    ...config.headers
  }

  return new Promise((resolve, reject) => {
    uni.request({
      url: `${envConfig.VUE_APP_BASE_API}${envConfig.VUE_APP_PARAM}${url}`,
      method,
      data,
      header: headers,
      success: (res) => {
        // 可以在此处添加统一响应处理
        resolve(res.data)
      },
      fail: (err) => {
        console.error('请求失败:', err)
        reject(err)
      }
    })
  })
}

// 创建快捷方法
export const http = {
  get: createRequest('GET'),
  post: createRequest('POST'),
  put: createRequest('PUT'),
  delete: createRequest('DELETE')
}